<?php
/******* SIDEBARS *********/

if ( function_exists('register_sidebar') ) 
register_sidebar(array('name'=>'Home Sidebar',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'Blog Sidebar',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'Page Sidebar',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'General Sidebar',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'Footer 1',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'Footer 2',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'Footer 3',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'Footer 4',
'before_widget' => '<div class="widget widget4">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));





/******* THEME FUNCTIONS *********/

function getTemplatePath() {
return get_bloginfo('template_directory');
}

function getRecentComments() {
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,100) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND
post_password = ''
ORDER BY comment_date_gmt DESC
LIMIT 5";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= "\n<ul>";
foreach ($comments as $comment) {
$output .= "\n<li><b>".strip_tags($comment->comment_author)
."</b> said<br/>" . "<a href=\"" . get_permalink($comment->ID) .
"#comment-" . $comment->comment_ID . "\" title=\"on " .
$comment->post_title . "\">" . strip_tags($comment->com_excerpt)
."</a></li>";
}
$output .= "\n</ul>";
$output .= $post_HTML;
return $output;
}


function custom_comment($comment, $args, $depth) {	
       $GLOBALS['comment'] = $comment; ?>

<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">

    	<span class="left"><?php echo get_avatar($comment,$size='80',$default='http://casamenteiras.com.br/wp-content/uploads/2010/03/avatar-casamenteiras.jpg' ); ?><br /><?php comment_author_link() ?></span>

    <?php if ($comment->comment_approved == '0') : ?>
    <em>Seu coment&aacute;rio est&aacute; aguardando modera&ccedil;&atilde;o.</em>
    <?php endif; ?>
    
    <?php comment_text() ?>
    
		<p class="meta"><?php comment_date('j \d\e F \d\e Y') ?> &agrave;s <?php comment_time() ?> <?php edit_comment_link('editar','&nbsp;&nbsp;',''); ?> (<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>)</p>

</li>
 
<?php } 


function get_thumbnail($id,$w,$h) {
$url = get_post_meta($id, 'thumbnail', true);
if($w == "") { $w="100"; }
if($h == "") { $h="100"; }
if($zc == "") { $zc="1"; }
if($q == "") { $q="100"; }
if($url == "") {
	global $post, $posts;
	$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
	$first_img = $matches [1] [0];
	if(empty($first_img)){ 
		$first_img = get_bloginfo('template_directory') . "/images/defaultImage.jpg";
	}
	if($thumbScript == "No") {
		$imageURL = $first_img;
	} else {
		$imageURL = get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$first_img.'&amp;w='.$w.'&amp;h='.$h.'&amp;zc='.$zc.'&amp;q='.$q.'';
	}
	return $imageURL;
} else {
	if($thumbScript == "No") {
		$imageURL = $url;
	} else {
		$imageURL = get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$url.'&amp;w='.$w.'&amp;h='.$h.'&amp;zc='.$zc.'&amp;q='.$q.'';
	}
	return $imageURL;
}
}


function get_thumbnail_noTimThumb($id,$w,$h,$class) {
$url = get_post_meta($id, 'thumbnail', true);
if($url == "") {
	global $post, $posts;
	$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
	$first_img = $matches [1] [0];
	if(empty($first_img)){ 
		$first_img = get_bloginfo('template_directory') . "/images/defaultImage.jpg";
	}
	$imageURL = '<span style="display:block;width:'.$w.'px;height:'.$h.'px;overflow:hidden;padding:0px;margin-botom:3px;" class="'.$class.'"><img style="height:100%" src="'.$first_img.'" alt="" /></span>';
	return $imageURL;
} else {
	$imageURL = '<span style="display:block;width:'.$w.'px;height:'.$h.'px;overflow:hidden;padding:0px;" class="'.$class.'"><img style="height:100%" src="'.$url.'" alt="" /></span>';
	return $imageURL;
}
}



function get_banner_image($id,$w,$h) {
$zc=1;
$q=100;
$url = get_post_meta($id, 'bannerImage', true);
if($url=="") { $url = get_post_meta($id,'thumbnail',true); }
if($url == "") {
	global $post, $posts;
	$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
	$first_img = $matches [1] [0];
	if(empty($first_img)){ 
		$first_img = get_bloginfo('template_directory') . "/images/defaultImage.jpg";
	}	
		$imageURL = get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$first_img.'&amp;w='.$w.'&amp;h='.$h.'&amp;zc='.$zc.'&amp;q='.$q.'';
	return $imageURL;
} else {
	$imageURL = get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$url.'&amp;w='.$w.'&amp;h='.$h.'&amp;zc='.$zc.'&amp;q='.$q.'';
	return $imageURL;
}
}


function get_banner_image_noTimThumb($id) {
$url = get_post_meta($id, 'bannerImage', true);
if($url=="") { $url = get_post_meta($id,'thumbnail',true); }
if($url == "") {
	global $post, $posts;
	$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
	$first_img = $matches [1] [0];
	if(empty($first_img)){ 
		$first_img = get_bloginfo('template_directory') . "/images/defaultImage.jpg";
	}
	return $first_img;
} else {
	return $url;
}
}


function listCats() {
$listCats = array();
$categories=  get_categories('hide_empty=0'); 
foreach ($categories as $cat) {
	$listCats[$cat->cat_ID] = $cat->cat_name;
}
return $listCats;
}

function smallerexcerpt($num) {
$limit = $num+1;
$excerpt = explode(' ', get_the_excerpt(), $limit);
array_pop($excerpt);
$excerpt = implode(" ",$excerpt)."...";
echo $excerpt;
}





/******* CUSTOM FIELDS *********/

$post_meta_boxes =
array(
"thumbnail" => array(
"name" => "thumbnail",
"std" => "",
"title" => "Thumbnail Image",
"description" => "Add the URL for the thumbnail image, this will get resized throughout the site if using timthumb option"), 

"bannerImage" => array(
"name" => "bannerImage",
"std" => "",
"title" => "Banner Image",
"description" => "If different from thumbnail, recommended size is 960 pixels width and 270 pixels height"), 

"bannerImageCredit" => array(
"name" => "bannerImageCredit",
"std" => "Photo Credit: Wikipedia",
"title" => "Banner Image Credit",
"description" => "If you want to add a credit to the image (or some other text in its place) add it here."),
);

$page_meta_boxes =
array(
"bannerImage" => array(
"name" => "bannerImage",
"std" => "",
"title" => "Banner Image",
"description" => "If different from thumbnail, recommended size is 960 pixels width and 270 pixels height"), 

"pageDescription" => array(
"name" => "pageDescription",
"std" => "",
"title" => "Page Description",
"description" => "The page description that appears under the page title"), 
);


function post_meta_boxes() {
global $post, $post_meta_boxes;

foreach($post_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);

if($meta_box_value == "")
$meta_box_value = $meta_box['std'];

echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';

echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';

echo'<input style="color:#666;" type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';

echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
}
}


function page_meta_boxes() {
global $post, $page_meta_boxes;

foreach($page_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);

if($meta_box_value == "")
$meta_box_value = $meta_box['std'];

echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';

echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';

echo'<input style="color:#666;" type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';

echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
}
}


function create_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'Post Options', 'post_meta_boxes', 'post', 'normal', 'high' );
add_meta_box( 'new-meta-boxes', 'Page Options', 'page_meta_boxes', 'page', 'normal', 'high' );
}
}


function save_postdata( $post_id ) {
global $post, $post_meta_boxes;

foreach($post_meta_boxes as $meta_box) {
// Verify
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}

if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}

$data = $_POST[$meta_box['name'].''];

if(get_post_meta($post_id, $meta_box['name'].'') == "")
add_post_meta($post_id, $meta_box['name'].'', $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'', true))
update_post_meta($post_id, $meta_box['name'].'', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'', get_post_meta($post_id, $meta_box['name'].'', true));
}
}

function save_pagedata( $post_id ) {
global $post, $page_meta_boxes;

foreach($page_meta_boxes as $meta_box) {
// Verify
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}

if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}

$data = $_POST[$meta_box['name'].''];

if(get_post_meta($post_id, $meta_box['name'].'') == "")
add_post_meta($post_id, $meta_box['name'].'', $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'', true))
update_post_meta($post_id, $meta_box['name'].'', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'', get_post_meta($post_id, $meta_box['name'].'', true));
}
}


add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
add_action('save_post', 'save_pagedata');





/******* THEME OPTIONS *********/

$themename = "NetworkWP";
$options = array (

array(    "name" => "<h1>NetworkWP Options</h1><p>This is the options page for the NetworkWP theme, developed by <a href='http://simon-north.com'>Simon North</a>, <a href='http://webdesign.simnorcomputing.com'>Simnor Web Design</a></p>",
        "type" => "title"),

array(    "name" => "",
        "type" => "documentation"),
        
array(    "type" => "open"),

array(    "name" => "General Options",
        "type" => "titleWithSaveButton"),

array(    "name" => "Style",
		 "desc" => "&nbsp;",
        "id" => "style",
        "std" => "",
		  "options" => array("Default (Blue)", "Green", "Brown", "Red", "Orange"),
        "type" => "select"),

array(    "name" => "Use Logo",
		 "desc" => "&nbsp;",
        "id" => "useLogo",
        "std" => "No",
		  "options" => array("Yes", "No"),
        "type" => "select"),
        
array(    "name" => "Logo Image",
		 "desc" => "Add the location to the logo image you'd like to use",
        "id" => "logo",
        "std" => getTemplatePath()."/images/logo.png",
        "type" => "text"),

array(    "name" => "Use TimThumb Script",
		 "desc" => "TimThumb is the script that resizes and resamples the thumbnails",
        "id" => "thumbScript",
		  "options" => array("Yes", "No"),
        "type" => "select"),

array(    "name" => "Use Text Shadows",
		 "desc" => "&nbsp;",
        "id" => "useShadows",
        "std" => "Yes",
		  "options" => array("Yes", "No"),
        "type" => "select"),
        
array(    "type" => "close"),

array(    "type" => "open"),

array(    "name" => "Navigation Options",
        "type" => "titleWithSaveButton"),
        
array(    "name" => "Exclude Pages from Menu",
		 "desc" => "Add the page ID's that you'd like to hide from the menu (comma separated)",
        "id" => "excludePages",
        "std" => "",
        "type" => "text"),
        
array(    "name" => "Exclude Categories from Menu",
		 "desc" => "Add the category ID's that you'd like to hide from the menu (comma separated)",
        "id" => "excludeCats",
        "std" => "",
        "type" => "text"),
        
array(    "name" => "RSS Link",
		 "desc" => "The link to the RSS Feed",
        "id" => "rssLink",
        "std" => get_bloginfo('rss2_url')."",
        "type" => "text"),
        
        
array(    "type" => "close"),

array(    "type" => "open"),

array(    "name" => "Sidebar and Footer Options",
        "type" => "titleWithSaveButton"),

array(    "name" => "Show Tabs in Sidebar",
		 "desc" => "&nbsp;",
        "id" => "showTabs",
        "std" => "Yes",
		  "options" => array("Yes", "No"),
        "type" => "select"),

array(    "name" => "Use General Sidebar",
		 "desc" => "Use the General Sidebar on all parts of the site",
        "id" => "useGeneralSidebar",
        "std" => "No",
		  "options" => array("Yes", "No"),
        "type" => "select"),
        
array(    "name" => "Footer Text",
		 "desc" => "The text you want in the footer. You may remove the Simnor Web Design link however <b>YOU MUST NOT</b> claim the design as your own",
        "id" => "footerText",
        "std" => "<p>COPYRIGHT &copy; 2009 SIMON NORTH<br/>DESIGNED by <a href='http://webdesign.simnorcomputing.com'>SIMNOR WEB DESIGN</a></p>",
        "type" => "textarea"),
        
        
array(    "type" => "close"),

array(    "type" => "open"),

array(    "name" => "Banner Options",
        "type" => "titleWithSaveButton"),

array(    "name" => "Featured Category",
		 "desc" => "&nbsp;",
        "id" => "featuredCat",
        "std" => "",
			"options" => listCats(),
        "type" => "select"),

array(    "name" => "Number of Featured Posts",
		 "desc" => "&nbsp;",
        "id" => "numberOfFeaturedPosts",
        "std" => "",
		  "options" => array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"),
        "type" => "select"),

array(    "name" => "Featured Sliding Interval",
		 "desc" => "&nbsp;",
        "id" => "featuredInterval",
        "std" => "",
		  "options" => array("1 second", "2 seconds", "3 seconds", "4 seconds", "5 seconds", "6 seconds", "7 seconds", "8 seconds", "9 seconds", "10 seconds", "Dont slide automatically"),
        "type" => "select"),

array(    "type" => "close"),

array(    "type" => "open"),

array(    "name" => "Homepage Options",
        "type" => "titleWithSaveButton"),

array(    "name" => "Home Layout",
		 "desc" => "&nbsp;",
        "id" => "homeLayout",
        "std" => "",
		  "options" => array("Default", "Blog"),
        "type" => "select"),

array(    "name" => "Number of Latest Posts",
		 "desc" => "&nbsp;",
        "id" => "numberOfLatestPosts",
        "std" => "",
		  "options" => array("3", "6", "9"),
        "type" => "select"),

array(    "name" => "Second Category",
		 "desc" => "&nbsp;",
        "id" => "homepageCat1",
        "std" => "",
			"options" => listCats(),
        "type" => "select"),

array(    "name" => "Number of Secondary Posts",
		 "desc" => "&nbsp;",
        "id" => "numberOfPostsCat1",
        "std" => "",
		  "options" => array("3", "6", "9"),
        "type" => "select"),

array(    "name" => "Third Category",
		 "desc" => "&nbsp;",
        "id" => "homepageCat2",
        "std" => "",
			"options" => listCats(),
        "type" => "select"),

array(    "name" => "Number of Thirdly Posts",
		 "desc" => "&nbsp;",
        "id" => "numberOfPostsCat2",
        "std" => "",
		  "options" => array("3", "6", "9"),
        "type" => "select"),


array(    "type" => "close"),

array(    "type" => "open"),

array(    "name" => "300 x 250 Advert Options",
        "type" => "titleWithSaveButton"),
        
array(    "name" => "Advert Link",
		 "desc" => "&nbsp;",
        "id" => "bigAdvertLink",
        "std" => "#",
        "type" => "text"),
        
array(    "name" => "Advert Image",
		 "desc" => "&nbsp;",
        "id" => "bigAdvertImage",
        "std" => get_bloginfo('template_directory')."/images/300x250ad.jpg",
        "type" => "text"),
        
array(    "name" => "Advert Alt Text",
		 "desc" => "&nbsp;",
        "id" => "bigAdvertAlt",
        "std" => "Advert",
        "type" => "text"),
        
array(    "name" => "Advert Heading",
		 "desc" => "&nbsp;",
        "id" => "bigAdvertHeading",
        "std" => "ADVERT",
        "type" => "text"),
        
array(    "name" => "Adsense Code",
        "id" => "bigAdvertAdsense",
        "std" => "",
        "desc" => "If you want to use adsense or another advert provider for this advert area then paste the code here",
        "type" => "textarea"),
        
array(    "type" => "close"),

array(    "type" => "open"),

array(    "name" => "125 x 125 Advert Options",
        "type" => "titleWithSaveButton"),
        
array(    "name" => "Advert Heading",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertHeading",
        "std" => "ADVERT",
        "type" => "text"),
        
array(    "name" => "Adsense Code",
        "id" => "smallAdvertAdsense",
        "std" => "",
        "desc" => "If you want to use adsense or another advert provider for this advert area then paste the code here",
        "type" => "textarea"),
        
array(    "name" => "Advert 1 Link",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertLink1",
        "std" => "",
        "type" => "text"),
        
array(    "name" => "Advert 1 Image",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertImage1",
        "std" => get_bloginfo('template_directory')."/images/125x125ad.jpg",
        "type" => "text"),
        
array(    "name" => "Advert 1 Alt Text",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertAlt1",
        "std" => "Advert",
        "type" => "text"),
        
array(    "name" => "Advert 2 Link",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertLink2",
        "std" => "",
        "type" => "text"),
        
array(    "name" => "Advert 2 Image",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertImage2",
        "std" => get_bloginfo('template_directory')."/images/125x125ad.jpg",
        "type" => "text"),
        
array(    "name" => "Advert 2 Alt Text",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertAlt2",
        "std" => "Advert",
        "type" => "text"),
        
array(    "name" => "Advert 3 Link",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertLink3",
        "std" => "",
        "type" => "text"),
        
array(    "name" => "Advert 3 Image",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertImage3",
        "std" => get_bloginfo('template_directory')."/images/125x125ad.jpg",
        "type" => "text"),
        
array(    "name" => "Advert 3 Alt Text",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertAlt3",
        "std" => "Advert",
        "type" => "text"),
        
array(    "name" => "Advert 4 Link",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertLink4",
        "std" => "",
        "type" => "text"),
        
array(    "name" => "Advert 4 Image",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertImage4",
        "std" => get_bloginfo('template_directory')."/images/125x125ad.jpg",
        "type" => "text"),
        
array(    "name" => "Advert 4 Alt Text",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertAlt4",
        "std" => "Advert",
        "type" => "text"),
        
array(    "name" => "Advert 5 Link",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertLink5",
        "std" => "",
        "type" => "text"),
        
array(    "name" => "Advert 5 Image",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertImage5",
        "std" => get_bloginfo('template_directory')."/images/125x125ad.jpg",
        "type" => "text"),
        
array(    "name" => "Advert 5 Alt Text",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertAlt5",
        "std" => "Advert",
        "type" => "text"),
        
array(    "name" => "Advert 6 Link",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertLink6",
        "std" => "",
        "type" => "text"),
        
array(    "name" => "Advert 6 Image",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertImage6",
        "std" => get_bloginfo('template_directory')."/images/125x125ad.jpg",
        "type" => "text"),
        
array(    "name" => "Advert 6 Alt Text",
		 "desc" => "&nbsp;",
        "id" => "smallAdvertAlt6",
        "std" => "Advert",
        "type" => "text"),
        
array(    "type" => "close"),

); 

function mytheme_add_admin() {

    global $themename, $shortname, $options;

    if ( $_GET['page'] == basename(__FILE__) ) {

        if ( 'save' == $_REQUEST['action'] ) {

                foreach ($options as $value) {
                    update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }

                foreach ($options as $value) {
                    if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }

                header("Location: themes.php?page=functions.php&saved=true");
                die;

        } else if( 'reset' == $_REQUEST['action'] ) {

            foreach ($options as $value) {
                delete_option( $value['id'] ); }

            header("Location: themes.php?page=functions.php&reset=true");
            die;

        }
    }

    add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');

}

function mytheme_admin() {

    global $themename, $shortname, $options;

    if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
    if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';

?>


<style type="text/css">
#message {
}
.wrap {
padding-top:0;
display:block;
clear:both;
position:relative;
font-family: Arial;
}
.wrap h2 {
font-size:16px;
display:block;
font-style: normal;
font-weight:bold;
color:#555;
background:#CCC;
margin:0!important;
overflow:hidden;
height:40px;
padding:0px;
position:relative;
cursor:pointer;
font-family: Arial;
}
.wrap h2 span {
float:left;
padding:0px 15px;
line-height:40px;
}
.wrap h2 input {
float:right;
height:50px;
border:none;
padding:10px 15px;
background:#333;
color:#FFF;
position:absolute;
top:-8px;
right:-5px;
z-index:1000;
}
.wrap h2 input:hover {
background:#555;
color:#FFF;
}
.wrap h2.heading {
margin:0px;
padding:0px;
height:120px;
display:block;
overflow: hidden;
width:100%;
}
p.heading {
margin-top:0px;
font-size:12px;
padding:10px;
display:block;
font-family: Arial;
font-style: normal;
color:#555;
background:#CCC;
}
.block {
background: #FFF;
overflow:hidden;
margin:0 0 10px 0;
}
.block p {
padding:0px 20px 10px 20px;
}
.block .inner {
padding:10px;
margin-top:0px;
border-left:2px solid #CCC;
border-right:2px solid #CCC;
border-bottom:2px solid #CCC;
}
p input {
clear:both;
margin-top:10px;
width:400px;
}
small {
display:block;
clear:both;
margin-left:210px;
}
p label {
font-weight:bold;
width:200px;
padding:5px 10px 0 0;
line-height:20px;
display:block;
float:left;
}
p textarea {
clear:both;
margin-top:10px;
width:500px;
height:100px;
}

.buttons {
font-size:16px;
display:block;
font-family: Arial;
font-style: normal;
font-weight:bold;
color:#555;
margin:0;
overflow:hidden;
height:40px;
padding:0px;
position:relative;
}

.buttons input {
float:right;
height:50px;
border:none;
padding:10px 15px;
background:#333;
color:#FFF;
position:absolute;
top:-8px;
right:-5px;
}

.buttons input:hover {
background:#555;
color:#FFF;
}

#message {
position:absolute;
top:0px;
right:0px;
padding:10px 20px;
}
#message p {
padding:0px;
}
.innerDocumentation p {
padding:0px;
margin:0 0 20px 0;
}
.innerDocumentation ul {
margin:0 0 20px 0;
}
.innerDocumentation h3 {
font-size:20px;
}
.innerDocumentation h4 {
margin:0 0 5px 0;
color:#999;
}
</style>




<div class="wrap">
<script type="text/javascript">
	jQuery(function() {
		jQuery('.block .inner').hide();
		jQuery('.block h2').click(function() {
			jQuery(this).parent('.block').find('.inner').slideToggle();
		});
		jQuery('.block h2').hover(function() {
			jQuery(this).css({"background":"#ddd", "color":"#555"});
		}, function() {
			jQuery(this).css({"background":"#CCC", "color":"#555"});
		});
	});
</script>



<form method="post">

<?php foreach ($options as $value) {

switch ( $value['type'] ) {

case "open":
?>
<div class="block">

<?php break;

case "close":
?>

</div></div>

<?php break;

case "title":
?>
<?php echo $value['name']; ?>

<?php break;


case "documentation":
?>
<div class="block">
<h2><span>Network WP Documentation</span></h2>
<div class="inner innerDocumentation">

<p>Firstly, thank you for purchasing Network WP. This is the documentation for Network WP, this page should help you get the template set up correctly. You should be using Wordpress 2.8 for this template. </p>

<h3>Initial Steps</h3>
<h4>Install and Activate Template</h4>
<p>To install this template, upload the NetworkWP folder to the wp-content/themes/ directory on your web server. Then go to your wordpress admin and navigate to the Appearance page and click the activate link under the image associated with Network WP.</p>

<p>For full details on this please follow this <a href="http://codex.wordpress.org/Using_Themes#Adding_New_Themes">tutorial</a>.</p>

<h4>Creating Posts</h4>
<p>You add posts as normal in Wordpress, however we have a few extra options with this template which you can use that aren't available normally so we'll look into them now.
Under the post editor you should see a section titled "Post Options". Under which you should see a form that enables you to add a thumbnail image, banner image and banner image credit. If you don't add a thumbnail image here then the template will grab the first image from the post and use that.</p>

<h4>Creating Pages</h4>
<p>You add pages as normal in Wordpress, however we have a few extra options with this template which you can use that aren't available normally so we'll look into them now.
Under the post editor you should see a section titled "Page Options". Under which you should see a form that enables you to add a banner image and page description. The page description is the text that shows up under the title when viewing the page.</p>

<h3>Template Options</h3>
<p>In the Wordpress admin section navigate to Appearance and then click the "NetworkWP Options" link. This will bring up a page with all the options you can change for the template. When you navigate to this page you'll see a bunch of headings, click on one of these headings and the appropriate options for that section will show. Click any of the Save Changes buttons to well, save your changes.</p>
<p>Here is an explanation of each of the fields:</p>

<h4>General Options</h4>
<ul>
<li><b>Style</b> There are 5 colour options with this template, you can choose which one you like from the select box.</li>
<li><b>Use Logo</b> The template has the option to use a logo or text, the text option will grab the value you set in the Blog Title field on the Settings page</li>
<li><b>Logo Image</b> If you choose to use the logo option then add the URL to the image here or replace the logo.png image in the images folder</li>
<li><b>Use TimThumb Script</b> Some hosts don't allow you to use GD on their servers which is what this uses, so if images don't show up on your site then select No</li>
<li><b>Use Text Shadows</b> The template uses text shadows on the menus and some of the headings. If you don't want this then select No, you'll also need to select No if you want the css of the site to validate.</li>
</ul>

<h4>Navigation Options</h4>
<ul>
<li><b>Exclude Pages from Menu</b> If you don't want some pages to show in the menu then add the page ID's here, separated by commas</li>
<li><b>Exclude Categories from Menu</b> If you don't want some categories to show in the menu then add the page ID's here, separated by commas</li>
</ul>

<h4>Sidebar and Footer Options</h4>
<ul>
<li><b>Show Tabs in Sidebar</b> These are the Latest, Tags and Comments tabs on the sidebars</li>
<li><b>Use General Sidebar</b> If you want to show the same sidebar throughout the site then select Yes here and add your widgets to the General sidebar</li>
<li><b>Footer Text</b> Here you can change the text you want to show in the footer. You're allowed to remove the credit to me, however YOU MAY <b>NOT</b> claim the work as your own</li>
</ul>

<h4>Banner Options</h4>
<ul>
<li><b>Featured Category</b> This is the category you want to show in the featured slider</li>
<li><b>Number of Featured Posts</b> Choose how many posts you want to show in the featured slider</li>
<li><b>Featured Sliding Interval</b> How long you want each slider to show on the featured slider</li>
</ul>

<h4>Homepage Options</h4>
<ul>
<li><b>Home Layout</b> The default option will show the 3 sections on the homepage. The blog option will list the latest posts.</li>
<li><b>Number of Latest Posts</b> If using the default option, choose how many posts you want to show in the first block (Latest Posts)</li>
<li><b>Second Category</b> The category for the second block of posts on the homepage</li>
<li><b>Number of Secondary Posts</b> The number of posts you want to show in the second block</li>
<li><b>Third Category</b> The category for the third block of posts on the homepage</li>
<li><b>Number of Thirdly Posts</b> The number of posts you want to show in the third block</li>
</ul>

<h4>Advert Options, see below</h4>

<h3>Setting up the advert widgets</h3>
<p>You can add adverts to your sidebars through the Widgets page. You can only use these widgets once so if you want to show them throughout the website then choose to use the General sidebar in the theme options. You can setup the adverts images, links and alt text on the theme options page. If you want to show Adsense code or another advert providers embed code instead then add the code into the "Adsense Code" fields of the options page.</p>

<h3>Setting up Threaded Comments</h3>
<p>Go to the wordpress admin area and navigate to the Settings -> Discussion page and check the "Enable threaded (nested) comments" box, change the depth level from 5 to 3 and then click the Save Changes button.</p>

<h3>Additional Help</h3>
<p>If you need any additional help please do not hesitate to contact me via my <a href="http://themeforest.net/user/simnor">profile page</a> on ThemeForest. Customizations are available at a cost, contact me for more details.</p>

</div>
</div>

<?php break;


case "titleWithSaveButton":
?>
<h2><span><?php echo $value['name']; ?></span><input name="save" type="submit" value="Save changes" /></h2><div class="inner">

<?php break;

case 'text':
?>

<p><label><?php echo $value['name']; ?></label>
<input style="border:1px solid #CCCCCC;padding:4px;width:550px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" />
<small> <?php echo $value['desc']; ?></small></p>

<?php 
break;

case 'textarea':
?>

<p><label><?php echo $value['name']; ?></label>
<textarea style="border:1px solid #CCCCCC;padding:4px;width:550px;" name="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings($value['id']) ); } else { echo $value['std']; } ?></textarea>
<small> <?php echo $value['desc']; ?></small>
</p>


<?php
break;

case 'select':
?>

<p><label><?php echo $value['name']; ?></label>
<select style="border:1px solid #CCCCCC;padding:4px;width:550px;height:30px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>"><?php foreach ($value['options'] as $option) { ?><option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?></select>
<small> <?php echo $value['desc']; ?></small>
</p>


<?php
break;

case "checkbox":
?>

<p><label><?php echo $value['name']; ?></label>
<? if(get_settings($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = ""; } ?>
<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
<small><?php echo $value['desc']; ?></small>
</p>
<?php         break;


}
}
?>


<div class="block">
<div class="buttons">
<input name="save" type="submit" value="Save changes" />
<input type="hidden" name="action" value="save" />
</form>
<form method="post">
<input  style="right:120px;" name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</form></div>
</div>
</div>
<?php
}

add_action('admin_menu', 'mytheme_add_admin'); ?>
<?php 




/******* WIDGETS *********/
function smallAdverts() {
?>
<div class="widget advertWidget smallAds">
				<?php if(get_option("smallAdvertHeading") == "") {} else { ?><h3><?php echo get_option("smallAdvertHeading"); ?></h3><?php } ?>
				<?php if(get_option("smallAdvertAdsense") == "") { ?>
					<?php if(get_option("smallAdvertLink1") == "") { } else { ?><a class="advert1" href="<?php echo get_option("smallAdvertLink1"); ?>"><img src="<?php echo get_option("smallAdvertImage1"); ?>" alt="<?php echo get_option("smallAdvertAlt1"); ?>" /></a><?php } ?>
					<?php if(get_option("smallAdvertLink2") == "") { } else { ?><a class="advert2" href="<?php echo get_option("smallAdvertLink2"); ?>"><img src="<?php echo get_option("smallAdvertImage2"); ?>" alt="<?php echo get_option("smallAdvertAlt2"); ?>" /></a><?php } ?>
					<?php if(get_option("smallAdvertLink3") == "") { } else { ?><a class="advert3" href="<?php echo get_option("smallAdvertLink3"); ?>"><img src="<?php echo get_option("smallAdvertImage3"); ?>" alt="<?php echo get_option("smallAdvertAlt3"); ?>" /></a><?php } ?>
					<?php if(get_option("smallAdvertLink4") == "") { } else { ?><a class="advert4" href="<?php echo get_option("smallAdvertLink4"); ?>"><img src="<?php echo get_option("smallAdvertImage4"); ?>" alt="<?php echo get_option("smallAdvertAlt4"); ?>" /></a><?php } ?>
					<?php if(get_option("smallAdvertLink5") == "") { } else { ?><a class="advert5" href="<?php echo get_option("smallAdvertLink5"); ?>"><img src="<?php echo get_option("smallAdvertImage5"); ?>" alt="<?php echo get_option("smallAdvertAlt5"); ?>" /></a><?php } ?>
					<?php if(get_option("smallAdvertLink6") == "") { } else { ?><a class="advert6" href="<?php echo get_option("smallAdvertLink6"); ?>"><img src="<?php echo get_option("smallAdvertImage6") ?>" alt="<?php echo get_option("smallAdvertAlt6"); ?>" /></a><?php } ?>
				<?php } else { ?>
					<?php echo stripslashes(get_option("smallAdvertAdsense")); ?>
				<?php } ?>
			</div>
<?php } register_sidebar_widget('125 x 125 Adverts', 'smallAdverts');



function bigAdvert() {
?>
<div class="widget advertWidget">
				<?php if(get_option("bigAdvertHeading") == "") {} else { ?><h3><?php echo get_option("bigAdvertHeading"); ?></h3><?php } ?>
				<?php if(get_option("bigAdvertAdsense") == "") { ?>
					<a href="<?php echo get_option("bigAdvertLink"); ?>"><img src="<?php echo get_option("bigAdvertImage"); ?>" alt="<?php echo get_option("bigAdvertAlt"); ?>" /></a>
				<?php } else { ?>
					<?php echo stripslashes(get_option("bigAdvertAdsense")); ?>
				<?php } ?>
			</div>
<?php } register_sidebar_widget('300 x 250 Advert', 'bigAdvert');